package com.gitee.wsl.coroutines.executor.runnable

import com.gitee.wsl.concurrent.api.RunnableWithResult

/**
 * 对Runnable对象的包装类,是具有可比较线程优先级的Runnable
 */
class PriorityRunnableWithResult<T>(
    val priority: Int,
    private val runnable: RunnableWithResult<T>
) : RunnableWithResult<T> by runnable, Comparable<PriorityRunnableWithResult<*>> {

    override fun compareTo(other: PriorityRunnableWithResult<*>): Int {
        return if (this.priority < other.priority) {
            1
        } else if (this.priority > other.priority) {
            -1
        } else {
            0
        }
    }
}

fun<T> RunnableWithResult<T>.onPriority(priority: Int) = PriorityRunnableWithResult(priority,this)